package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void add(DishDTO dishDTO) {
//      从DishDTO获得Dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.add(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
//            将每一个dishid放入dishflavors中
            for (DishFlavor df : flavors) {
                df.setDishId(dishId);
            }
//            批量插入
            dishFlavorMapper.add(flavors);
        }
    }

    @Override
    public PageResult find(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dish);
        Page<DishVO> p=dishMapper.find(dish);
        PageResult pageBean=new PageResult(p.getTotal(),p.getResult());
        return  pageBean;
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {

        for (Long id : ids) {
            //        判断菜品是否在售
            DishVO d = dishMapper.findById(id);
            if(d==null){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //        是否有套餐关联
            List<SetmealDish> meals = setmealDishMapper.findById(id);
            if (meals == null || meals.size() == 0) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            //        删除菜品表中的数据
            dishMapper.delete(id);
            //        删除口味表中的数据
            dishFlavorMapper.delete(id);
        }


    }

    @Override
    public DishVO findById(Long id) {
        DishVO dishVO = dishMapper.findById(id);
        dishVO.setFlavors(dishFlavorMapper.findByDishId(id));
        return dishVO;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        dishFlavorMapper.delete(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor df : flavors) {
            df.setDishId(dish.getId());
        }
        dishFlavorMapper.add(flavors);
    }

    @Override
    public List<DishVO> findByCategoryId(Long categoryId) {
        return dishMapper.findByCategoryId(categoryId);

    }

    @Override
    public List<DishVO> findByCategoryIdWithFlavour(Dish dish) {
        List<DishVO> dishVOS = dishMapper.findByCategoryId(dish.getCategoryId());

        for (DishVO d : dishVOS) {
            d.setFlavors(dishFlavorMapper.findByDishId(d.getId()));
        }
        return dishVOS;
    }

}
